package da;

import data.ResourceType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Stijn Verslycken, Jonas Van Den Bossche
 * @date 2007-2008
 */
public class ResourceTypeDA {

    private ResourceBundle bundle;
    private static ResourceTypeDA instance = null;

    private ResourceTypeDA() {
        bundle = ResourceBundle.getBundle("da.dbconsts");
    }

    public static ResourceTypeDA getInstance() {
        if (instance == null) {
            instance = new ResourceTypeDA();
        }
        return instance;
    }
    
    public Vector<ResourceType> getAllResourceTypes() {
        Vector<ResourceType> rts= null;
        ResourceType rt = null;
        
        String sql = bundle.getString("getAllResourceTypes");

        Connection dbcon = null;
        try {
            dbcon = DatabaseConnection.getInstance().getConnection();
            PreparedStatement prepstat = dbcon.prepareStatement(sql);
            try {

                ResultSet rs = prepstat.executeQuery();

                rts = new Vector<ResourceType>();
                while (rs.next()) {

                    rt = new ResourceType(rs.getString("name"), rs.getString("image_url"));
                    rts.add(rt);
                }

            } finally {
                prepstat.close();
                dbcon.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingDA.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("getAllResourceTypes error -->" + ex.toString());
        }

        return rts;
    } 
}
